<?php

/**
 * Error class for the shoehorn project
 * 
 * @package shoehorn
 * @subpackage classes
 */

/**#@+
 * Constants
 */
/**
 * Error related contstants
 */
define("SH_E_FATAL", 1); //fatal, kill the process and display the error
define("SH_E_SILENT", 2);  //silent, just log it
define("SH_E_WARN", 3); //just issue a warning and keep going


/**
 * Error Handling Class
 * 
 * Used to manage error messages from various SHOEHORN classes
 * I may want to incorporate php exception handling and error handling
 * in the future... rather than passing this function all over the place
 * 
 * @package shoehorn
 * @subpackage classes
 * @subpackage error
 *
 */
class Shoehorn_Error
{
	private $errors = array();
	
	/**
	 * Log an error
	 *
	 * @param integer $errID
	 * @param string $func
	 * @param integer $errType
	 * @param string $debStr
	 * @return void
	 */
	public function logError($errID, $func, $errType, $debStr = NULL)
	{
		$this->errors[$errType][] = array('func'=>$func, 'id'=>$errID, 
		'debug'=>$debStr);
	}
	
	/**
	 * Dump Errors
	 * 
	 * @param integer errType
	 */
	public function dumpErrors($errType=NULL)
	{
		echo("<pre>\n");
		if(isset($errType) && $errType!=NULL) {
			if(isset($this->errors[$errType])) {
				print_r($this->errors[$errType]);
			} else {
				echo('Invalid Error Type');
			}
		} else {
			print_r($this->errors);
		}
		echo("\n<pre>");
	}
	
	/**
	 * Get the HTML to display error messages, if needed
	 *
	 * @return string
	 */
    public function getErrorHTML()
    {
        //establish the variable full of html to return
        $errHTML = '<div class="shoehornErrors">';
        
        //only do stuff if we actually have error 
        if(isset($this->errors[3]) && count($this->errors[3])>0) {
            //let's look for the error code file for our locale
            $errFile = SHOEHORN_SCRIPT_FOLDER . '/locale/' . SHOEHORN_LOCALE . '/err.ini';
            //establish the error messages array
            $errMessages = array();
            //make sure the file exists
            if(file_exists($errFile)) {
                //now include the file
                $errMessages = parse_ini_file($errFile);
                //just in case, let's make the variable an array if we couldn't parse the ini file
                if($errMessages == false) {
                    $errMessages = array();
                }
            }
            //loop through the public error messages
            //remember, if we wanted debugging errors, we'd just call dumpErrors
            foreach($this->errors[3] as $err) {
                //get the string for the error or make one up
                if(isset($errMessages[$err['id']])) {
                    $tmpMsg = '<strong> Error ' . $err['id'] . ':</strong> ' . $errMessages[$err['id']];
                } else {
                    $tmpMsg = 'unknown error ' . $err['id'];
                }
                //add the actual line to the code
                $errHTML .= '<p class="shoehornError">' . $tmpMsg . '</p>';
            }
        //if we don't have an error, just return a blank string
        } else {
            return '';
        }
        $errHTML .= '</div>';
        
        //now return the html we've built
        return $errHTML;
    }
}